package com.yuanfeng.action.front.address.web;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanfeng.action.front.address.service.AddressService;
import com.yuanfeng.action.front.address.service.AddressServiceImpl;
import com.yuanfeng.pojo.Address;
import com.yuanfeng.pojo.Users;
import com.yuanfeng.util.WebUtil;
import org.apache.struts2.ServletActionContext;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
 * 收货地址模块的Action
 * @author yuanfeng
 */
public class AddressAction extends ActionSupport {

	private AddressService addressServiceImpl;

	public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {
		this.addressServiceImpl = addressServiceImpl;
	}

    /**
     * 查看某个用户的收货地址列表
     * @return
     */
	public String queryAddress(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//获取用户id
		Users user = (Users)request.getSession().getAttribute("user");
		List<Address> addrs = addressServiceImpl.queryAddress(user.getId());
		request.setAttribute("addrs",addrs);
		return "queryAddress";
	}

    /**
     * 添加收货地址
     * @return
     */
	public String addAddress(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Address addr = WebUtil.copyRequestToBean(request, Address.class);
		Users user = (Users)request.getSession().getAttribute("user");
		addr.setUserid(user.getId());
		addr = addressServiceImpl.add(addr);
		request.setAttribute("addr",addr);//存在request中就使用转发技术
		return "addAddress";
	}

    /**
     * 删除收货地址
     * @return
     */
	public String delAddress(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Address addr = WebUtil.copyRequestToBean(request, Address.class);
		addressServiceImpl.delete(addr);
		return this.queryAddress();
	}

    /**
     * 设置为默认收货地址
     * @return
     */
	public String changeToDefault(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Address addr = WebUtil.copyRequestToBean(request, Address.class);
		Users user = (Users)request.getSession().getAttribute("user");
		addr.setUserid(user.getId());
		addressServiceImpl.changeToDefault(addr);
		return this.queryAddress();
	}
}
